﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace GtsServer.Common
{
  public class SendEmail
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="bodyMsg">邮件正文</param>
        /// <param name="subject">邮件主题</param>
        /// <param name="toEmail">收件人地址</param>
        /// <param name="fromEmail">发件人地址</param>
        /// <param name="fromDisName">发件人昵称</param>
        /// <param name="emailHost">邮箱Host</param>
        /// <param name="emailNmae">发件人邮箱登录名称</param>
        /// <param name="emailPwd">发件人邮箱登录密码</param>
        /// <returns>成功返回True,失败返回false。异常抛出</returns>
        public void EmailSend(string bodyMsg, string subject, string toEmail, string fromEmail, string fromDisName, string emailHost, string emailNmae, string emailPwd)
        {
            try
            {
                MailMessage mainMsg = new MailMessage();
                mainMsg.Body = bodyMsg;//邮件正文
                mainMsg.Subject = subject;//邮件主题
                mainMsg.From = new MailAddress(fromEmail, fromDisName, Encoding.UTF8);//发件人邮箱地址，发件人昵称
                mainMsg.To.Add(toEmail);//收件人地址
                SmtpClient client = new SmtpClient();
                client.Host = emailHost;
                client.Credentials = new System.Net.NetworkCredential(emailNmae, emailPwd);//("jing6569704", "mmfssfmr");
                object obj = mainMsg;
                client.SendAsync(mainMsg, obj);
            }
            catch (Exception ex)
            {
                throw new Exception("向" + toEmail + "发送邮件异常（2期）", ex);
            }
        }
    }
}
